package org.example.cloud.config.web;


import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.*;


/**
 * spring mvc的配置
 *
 * @author fengshuonan
 * @date 2020/4/11 10:23
 */
@Configuration
@Import({cn.hutool.extra.spring.SpringUtil.class,/* CorsFilterConfiguration.class*/})
public class SpringMvcConfiguration implements WebMvcConfigurer {


    /**
     * json自定义序列化工具,long转string
     *
     * @author fengshuonan
     * @date 2020/12/13 17:16
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance).serializerByType(Long.TYPE, ToStringSerializer.instance);
    }

    /**
     * 配置项目拦截器
     *
     * @author fengshuonan
     * @date 2020/12/18 9:43
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(authJwtTokenSecurityInterceptor);
//        registry.addInterceptor(permissionSecurityInterceptor);
    }

    /**
     * 静态资源映射
     *
     * @author fengshuonan
     * @date 2021/1/16 21:45
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/pages/wechat/");
//        registry.addResourceHandler("/**").addResourceLocations("classpath:/pages/");
    }

    /**
     * 跨域
     *
     * @author yanghuan
     * @date 2024/6/3 21:45
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                .allowedOriginPatterns("*") //支持域
                // 设置允许的请求方式
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                // 设置允许的header属性
                .allowedHeaders("*")
                // 是否允许cookie
                .allowCredentials(true)
                // 设置允许跨域的时长
                .maxAge(3600);
    }
    /**
     * 设置首页
     *
     * @author yanghuan
     * @date 2024/6/3 21:45
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:login.html");
    }
}
